// mircoApp/local_life/apply_store/apply_store.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_id:"",
    keyword: "",
    list:[],
    page:1,
    total_page:1,
    point:{
      lat:'',
      lng:""
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.goods_id = options.goods_id;
    let address_point = wx.getStorageSync("address-point");
    if (address_point) {
      this.data.point.lng = address_point.point.lng;
      this.data.point.lat = address_point.point.lat;
    }
    this.getData();
  },
  goto(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/mircoApp/local_life/store/index/index?id='+id,
    })
  },
  search(){
    this.getData();
  },
  onChange(e) {
    this.setData({
      keyword: e.detail,
    });
  },
  getData(page=1){
    let keyword = this.data.keyword;
    let json = { goods_id:this.data.goods_id*1,keyword,page};
    if (this.data.point.lng) {
      console.log('wq=qw=wq=qw=wq');
      json.latitude = this.data.point.lat * 1;;
      json.longitude = this.data.point.lng * 1;;
    }
    app._postNetWork({
      url:app.getNetAddresss("plugin.local-life.api.store.match-store"),
      data:json,
      success:resdata=>{
        let {result,msg,data} = resdata.data;
        if(result){
          let list = page == 1? (data.data||[]) : this.data.list.concat(data.data);
          this.setData({list});
          this.data.page = data.current_page;
          this.data.total_page = data.last_page;
        }else{
          app.tips(msg);
        }
      }
    })
  },
  navigation(event) { //查看商家的地址定位信息
    let latitude = event.currentTarget.dataset.latitude;
    let longitude = event.currentTarget.dataset.longitude;
    let store_name = event.currentTarget.dataset.store_name;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      name: store_name,
      scale: 18
    });
  },
  call_phone(event) {
    let phoneNumber = event.currentTarget.dataset.mobile;
    wx.makePhoneCall({
      phoneNumber,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.page>=this.data.total_page) return;
    let page = this.data.page + 1;
    this.getData(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})